#include "AdminHost.h"
#include "AdminConfiguration.h"

AgoraAdmin::Host::Host()
{
	// load du name
	this->_name = Configuration::GetConfiguration()->GetDatabase()->SelectHostName();
	//load ban list
	IDatabase::DbResult*	result = Configuration::GetConfiguration()->GetDatabase()->SelectHostBanListUser();
	for (unsigned int idx = 0; idx < result->size(); idx++)
		this->_banListLogin.push_back((*result->at(idx))["idUser"]->GetValueInt());
	delete result;
}
const std::string&				AgoraAdmin::Host::GetName()
{
	return this->_name;
}

void							AgoraAdmin::Host::SetName(const std::string& name)
{
	//modif dans la base
	Configuration::GetConfiguration()->GetDatabase()->UpdateHostName(name);
	//modif mem
	this->_name = name;
}

const std::list<unsigned int>&	AgoraAdmin::Host::GetBanListUser()
{
	return this->_banListLogin;
}

bool							AgoraAdmin::Host::UserIsBan(unsigned int idUser)
{
	std::list<unsigned int>::iterator	iteCurrent = this->_banListLogin.begin();
	std::list<unsigned int>::iterator	iteEnd = this->_banListLogin.end();

	while (iteCurrent != iteEnd)
	{
		if ((*iteCurrent) == idUser)
			return true;
		++iteCurrent;
	}
	return false;
}

void							AgoraAdmin::Host::AddBanListUser(unsigned int idUser)
{
	this->RemoveBanListUser(idUser);
	//ajout dans la base
	Configuration::GetConfiguration()->GetDatabase()->InsertHostBanListUser(idUser);
	this->_banListLogin.push_back(idUser);
}

void							AgoraAdmin::Host::RemoveBanListUser(unsigned int idUser)
{
	// remove de la base
	Configuration::GetConfiguration()->GetDatabase()->DeleteHostBanListUser(idUser);
	this->_banListLogin.remove(idUser);
}

bool							AgoraAdmin::Host::Authentification(const std::string& login, const std::string& password)
{
	IDatabase::DbResult* result = Configuration::GetConfiguration()->GetDatabase()->SelectUser(login);
	if (result->size() && this->UserIsBan((*result->at(0))["id"]->GetValueInt()))
		return false;
	if (result->size() && (*result->at(0))["password"]->GetValueString() == password)
		return true;
	return false;
}